package com.aio.news.category.fragment;

import java.util.List;

import org.apache.http.NameValuePair;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;

import com.aidi.lib.TAInjectView;
import com.aio.news.BaseFragment;
import com.aio.news.R;
import com.aio.news.category.adapter.CategoryAdapter;
import com.aio.news.model.CategoryModel;
import com.aio.news.net.HttpUtils.Method;
import com.aio.news.net.HttpUtils.RequestState;

public class CategoryFragment extends BaseFragment {

	@TAInjectView(id=R.id.gridview)
	private GridView mGridView;
	
	private RequestState mState = new RequestState("category");
	
	private CategoryAdapter mAdapter;
	
	@Override
	public View initView(LayoutInflater inflater) {
		return inflater.inflate(R.layout.category_layout, null);
	}

	@Override
	public List<NameValuePair> getParams(RequestState state) {
		return null;
	}

	@Override
	public void RequestSuccess(String response, RequestState state) {
		if(!TextUtils.isEmpty(response)){
			CategoryModel model = mGson.fromJsonWithNoException(response, CategoryModel.class);
			if(model != null && model.mItems != null && !model.mItems.isEmpty()){
				mAdapter.addItems(model.mItems);
			}
		}
		mState.isRequestServer = false;
	}

	@Override
	public void RequestFail(String errString, RequestState state) {
		mState.isRequestServer = false;
	}

	@Override
	public void afterCreated() {
		mAdapter = new CategoryAdapter(this);
		mGridView.setAdapter(mAdapter);
		getMessage(mState, Method.GET);
	}

}
